package mo.tags.disjointset;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class L128 {

    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int max = 1;
        Map<Integer, Integer> cnt = new HashMap<>();
        Arrays.sort(nums);
        for (int num : nums) {
            if (cnt.containsKey(num-1)) {
                int cur = cnt.get(num-1) + 1;
                cnt.put(num, cur);
                max = Math.max(max, cur);
            } else {
                cnt.put(num, 1);
            }
        }
        return max;
    }

}
